#include <QCheckBox>
#include "ItemBool.h"

QWidget *ItemBool::createWidget(WidgetStyle s)
{
    auto w = new QCheckBox;
    w->setText(objectName());
    connect(w, &QCheckBox::clicked, this, &ItemBool::onWidgetEditedWrapper);
    return w;
}

void ItemBool::update(QWidget *widget, UpdateOption option)
{
    QCheckBox *checkBox = qobject_cast<QCheckBox*>(widget);
    if (checkBox != nullptr)
    {
        if (option == UpdateOption::WidgetContent)
        {
            checkBox->setChecked(m_data);
        }
        else if (option == UpdateOption::Item)
        {
            this->m_data = checkBox->isChecked();
            qInfo() << "update: " << this->m_data;
        }
    }
    else
    {
        qWarning() << "";
    }
}

void ItemBool::writeToData(QDataStream &stream) const
{
    stream << m_data;
}

void ItemBool::readFromData(QDataStream &stream)
{
    stream >> m_data;
    emit itemEdited();
}
